//
//  MyUserInfoView.m
//  YXFinance
//
//  Created by uplooking on 2018/10/22.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "MyUserInfoView.h"

@implementation MyUserInfoView

- (void)willMoveToSuperview:(UIView *)newSuperview{
    
    [super willMoveToSuperview:newSuperview];
    
    [self initView];
}

-(void)setModel:(UserModel *)model{
    
    _model = model;
    
    [self initView];
    
}

- (void)initView{
    
    for (UIView * item in self.subviews) {
        
        [item removeFromSuperview];
    }
    
    [self setBackgroundColor:HexRGBA(0xffffff, 1)];
    
    if (_model) {
        
        [self initInfoViewInitWithModel:_model];
        
    }else{
        
        [self initLoginView];
    }
    
}

- (void)initInfoViewInitWithModel:(UserModel *)model{
    
    UIImageView * imageView = [[UIImageView alloc]init];
    [self addSubview:imageView];
    [imageView.layer setCornerRadius:35 * kAutoScanleX];
    [imageView.layer setMasksToBounds:YES];
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage: [UIImage imageNamed:@"icon_bianji_ziliao_touxiang"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(70 * kAutoScanleX, 70 * kAutoScanleX));
    }];
    
    
    
    UILabel * labelTitle = [[UILabel alloc]init];
    [labelTitle setText:model.nickname];
    [labelTitle setTextColor:HexRGBA(0x000000, 1)];
    [labelTitle setFont:[UIFont systemFontOfSize:18 * kAutoScanleX]];
    [labelTitle setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15 * kAutoScanleX);
        make.top.equalTo(imageView).offset(10 * kAutoScanleX);
    }];
    
    
    UILabel * labelSubTitle = [[UILabel alloc]init];
    [labelSubTitle setText:@"查看或编辑个人信息"];
    [labelSubTitle setTextColor:HexRGBA(0xbc9a6d, 1)];
    [labelSubTitle setFont:[UIFont systemFontOfSize:12 * kAutoScanleX]];
    [labelSubTitle setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelSubTitle];
    [labelSubTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15 * kAutoScanleX);
        make.top.equalTo(labelTitle.mas_bottom).offset(15 * kAutoScanleX);
    }];
    
    
    UIImageView * aimageView = [[UIImageView alloc]init];
    [self addSubview:aimageView];
    [aimageView setImage:[UIImage imageNamed:@"icon_arrow"]];
    [aimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(9 * kAutoScanleX, 15 * kAutoScanleX));
    }];
    
    UIButton * button = [[UIButton alloc]init];
    [button addTarget:self action:@selector(buttonInfoAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

}

- (void)initLoginView{
    
    UIImageView * imageView = [[UIImageView alloc]init];
    [self addSubview:imageView];
    [imageView setImage:[UIImage imageNamed:@"icon_bianji_ziliao_touxiang"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(70 * kAutoScanleX, 70 * kAutoScanleX));
    }];

    
    
    UILabel * labelTitle = [[UILabel alloc]init];
    [labelTitle setText:@"登录/注册"];
    [labelTitle setTextColor:HexRGBA(0x000000, 1)];
    [labelTitle setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [labelTitle setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15 * kAutoScanleX);
        make.centerY.equalTo(imageView);
    }];
    
    
    UIImageView * aimageView = [[UIImageView alloc]init];
    [self addSubview:aimageView];
    [aimageView setImage:[UIImage imageNamed:@"icon_arrow"]];
    [aimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(9 * kAutoScanleX, 15 * kAutoScanleX));
    }];

    UIButton * button = [[UIButton alloc]init];
    [button addTarget:self action:@selector(buttonLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    
}

- (void)buttonLoginAction:(UIButton *)sender{
    
    if (self.blockLoginClick) {
        
        self.blockLoginClick();
    }
    
}


- (void)buttonInfoAction:(UIButton *)sender{
    
    if (self.blockUserInfoClick) {
        
        self.blockUserInfoClick();
    }
    
}

@end
